$(function () {

  var id = getSearch().key
  console.log(id)
  $.ajax({
    url: '/product/queryProductDetail',
    type: 'get',
    data: {
      id: id
    },
    success: function (info) {
      console.log(info)
      var size = info.size
      size = size.split('-')
      var arr = []
      for (var i = +size[0]; i <= size[1]; i++) {
        arr.push(i)
      }
      info.sizeArray = arr
      console.log(arr)
      $('.mui-scroll').html(template('tmp', info))
      //初始化轮播图
      var gallery = mui('.mui-slider')
      gallery.slider({
        interval: 2000 //自动轮播周期，若为0则不自动播放，默认为0；
      })
      //初始化按键
      mui('.mui-numbox').numbox()

      $('.size span').on('click', function () {
        $(this).addClass('active').siblings().removeClass('active')
      })
    }
  })

  $('.addCart').on('click', function () {
    var size = $('.size span.active').text()
    if (!size) {
      mui.toast('请选择尺码')
      return
    }
    var product = $('.banner').data('id')
    var num = mui('.mui-numbox').numbox().getValue()
    console.log(product)
    console.log(num)
    console.log(size)
    $.ajax({
      url: '/cart/addCart',
      type: 'post',
      data: {
        productId: id,
        num: num,
        size: size
      },
      success: function (info) {
        console.log(info)
        if (info.error === 400) {
          mui.confirm('添加失败,请先登录', '温馨提示', ['前去登录', '继续浏览'], function (e) {
            console.log(e);
            if (e.index === 0) {
              location.href = 'login.html?backUrl=' + location.href
            }
          })
        }
        if (info.success) {
          mui.confirm('加入购物车成功', '温馨提示', ['到购物车', '继续浏览'], function (e) {
            console.log(e);
            if (e.index === 0) {
              location.href = 'shopcart.html'
            }
          })
        }
      }
    })
  })
})